//
//  MAMineView.m
//  MateAdv
//
//  Created by 茶 on 2023/9/15.
//

#import "MAMineView.h"

@interface MAMineView ()

@property (nonatomic, strong) UIImageView *headView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *numberLabel;

@property (nonatomic, strong) UILabel *vipLabel;
@property (nonatomic, strong) UILabel *vipDetailLabel;
@property (nonatomic, strong) UIButton *payBtn;
@property (nonatomic, strong) UIImageView *tagView;

@end

@implementation MAMineView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initInterface];
    }
    return self;
}

- (void)initInterface {
    [UIImageView ma_imgViewWithSuperView:self imgName:@"Mine_Back" makeMasonryBlock:^(MASConstraintMaker * _Nonnull make) {
        make.left.top.right.mas_equalTo(0);
    }];
    
    _headView = [UIImageView new];
    _headView.ma_cornerRadius = 44;
    [self addSubview:_headView];
    [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.ma_offset(70);
        make.size.ma_offset(88);
    }];
    
    _nameLabel = [UILabel ma_labelWithSuperView:self text:@"用户名" textFont:MAFontScaleSemibold(18) textColor:MAColor(@"#FF0071") textAlignment:NSTextAlignmentCenter makeMasonryBlock:^(MASConstraintMaker * _Nonnull make) {
        make.top.ma_offset(176);
        make.centerX.mas_equalTo(0);
    }];
    
    _numberLabel = [UILabel ma_labelWithSuperView:self text:@"ID:000000" textFont:MAFontScaleRegular(14) textColor:MAColor(@"#333333") textAlignment:NSTextAlignmentCenter makeMasonryBlock:^(MASConstraintMaker * _Nonnull make) {
        make.top.ma_offset(203);
        make.centerX.ma_offset(-5);
    }];
    
    MAWeakify(self);
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"Mine_Copy"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(copyEvent) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        MAStrongify(self);
        make.centerY.mas_equalTo(_numberLabel);
        make.left.mas_equalTo(self.numberLabel.mas_right).ma_offset(8);
    }];
    
    UIView *vipView = [UIView new];
    vipView.backgroundColor = [UIColor colorWithGradientStyle:UIGradientStyleLeftToRight withFrame:CGRectMake(0, 0, MARatioWidth(351), MARatioWidth(78)) andColors:@[MAColor(@"#FF0071"), MAColor(@"#E871FF")]];
    vipView.layer.cornerRadius = MARatioWidth(16);
    vipView.layer.borderColor = UIColorWhite.CGColor;
    vipView.layer.borderWidth = 1;
    vipView.clipsToBounds = YES;
    [self addSubview:vipView];
    [vipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.width.ma_offset(351);
        make.height.ma_offset(78);
        make.top.ma_offset(239);
    }];
    
    UIImageView *vipImgView = [UIImageView ma_imgViewWithSuperView:vipView imgName:@"Mine_Crown" makeMasonryBlock:^(MASConstraintMaker * _Nonnull make) {
        make.left.ma_offset(19);
        make.centerY.mas_equalTo(0);
    }];
    
    _vipLabel = [UILabel ma_labelWithSuperView:vipView text:@"VIP用户" textFont:MAFontSemibold(19) textColor:UIColorWhite textAlignment:0 makeMasonryBlock:^(MASConstraintMaker * _Nonnull make) {
        make.left.ma_offset(86);
        make.top.mas_equalTo(vipImgView);
    }];
    
    _vipDetailLabel = [UILabel ma_labelWithSuperView:vipView text:@"有限期至：2099-09-29" textFont:MAFontScaleRegular(14) textColor:UIColorWhite textAlignment:0 makeMasonryBlock:^(MASConstraintMaker * _Nonnull make) {
        make.left.ma_offset(86);
        make.bottom.mas_equalTo(vipImgView);
    }];
    
    _payBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_payBtn.titleLabel setFont:MAFontScaleMedium(14)];
    [_payBtn setBackgroundColor:[UIColor colorWithGradientStyle:UIGradientStyleLeftToRight withFrame:CGRectMake(0, 0, 68, 32) andColors:@[MAColor(@"#FFD4D4"), MAColor(@"#FBB8FF")]]];
    [_payBtn setTitle:@"续费" forState:UIControlStateNormal];
    [_payBtn setTitleColor:MAColor(@"#B025C6") forState:UIControlStateNormal];
    [vipView addSubview:_payBtn];
    [_payBtn addTarget:self action:@selector(payEvent) forControlEvents:UIControlEventTouchUpInside];
    [_payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.ma_offset(32);
        make.width.mas_greaterThanOrEqualTo(68);
        make.centerY.mas_equalTo(0);
        make.right.ma_offset(-16);
    }];
    _payBtn.layer.cornerRadius = MARatioWidth(16);
    _payBtn.layer.shadowColor = UIColorWhite.CGColor;
    _payBtn.layer.shadowOffset = CGSizeMake(0,0);
    _payBtn.layer.shadowOpacity = 0;
    _payBtn.layer.shadowRadius = 0.5;
    
    _tagView = [UIImageView ma_imgViewWithSuperView:self imgName:@"Mine_VipTag" makeMasonryBlock:^(MASConstraintMaker * _Nonnull make) {
        make.left.mas_equalTo(self.headView.mas_left).ma_offset(52);
        make.top.mas_equalTo(self.headView.mas_top).ma_offset(-18);
    }];
}

- (void)copyEvent {
    if (_model) {
        MABlock(self.copyBlock, [NSString stringWithFormat:@"%@", _model.user_id]);
    }
}

- (void)payEvent {
    MABlock(self.payBlock);
}

- (void)setModel:(MAUserModel *)model {
    _model = model;
    
    [_headView sd_setImageWithURL:[NSURL URLWithString:model.avatar]];
    _nameLabel.text = model.nickname;
    _numberLabel.text = [NSString stringWithFormat:@"ID:%@", model.user_id];
    
    if (model.vip_status.intValue > 0) {
        _tagView.hidden = NO;
        _vipLabel.text = @"VIP用户";
        if (model.vip_status.intValue == 2) {
            _vipDetailLabel.text = @"永久会员";
        } else {
            _vipDetailLabel.text = [NSString stringWithFormat:@"有限期至：%@", [[NSDate ma_dateWithTimestamp:model.vip_end_time] ma_stringWithFormat:@"yyyy-MM-dd"]];
        }
        
        [_payBtn setTitle:@"续费" forState:UIControlStateNormal];
    } else {
        _tagView.hidden = YES;
        _vipLabel.text = @"VIP立即解锁";
        _vipDetailLabel.text = @"高级玩家功能";
        [_payBtn setTitle:@"了解详情" forState:UIControlStateNormal];
    }
}

@end
